package com.ramos.kuaixiao.MyView;


import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.db.PriceDB;
import com.ramos.kuaixiao.db.ShopsDB;
import com.ramos.kuaixiao.db.StoreNumDB;
import com.ramos.kuaixiao.entity.BrandInfo;
import com.ramos.kuaixiao.entity.ShopTypeBean;
import com.ramos.kuaixiao.utils.ToastUtils;

import java.lang.reflect.Method;
import java.util.List;


public class CartDialog extends AlertDialog implements View.OnClickListener {

    private TextView sure;//确定按钮
    private TextView cancel;//取消按钮

    ImageView iv_assist_sub;
    ImageView iv_assist_plus;
    ImageView iv_sub;
    ImageView iv_plus;
    TextView tv_assist_unit;
    TextView tv_ware;
    EditText et_assist_number;
    EditText et_number;
    BrandInfo brandInfo;
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;

    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    /**
     * 初始化界面控件
     */
    private void initView() {
        sure = (TextView) findViewById(R.id.dialog_sure);
        cancel = (TextView) findViewById(R.id.dialog_cancel);
        iv_assist_sub = (ImageView) findViewById(R.id.iv_assist_sub);
        iv_assist_sub.setOnClickListener(this);
        iv_assist_plus = (ImageView) findViewById(R.id.iv_assist_plus);
        iv_assist_plus.setOnClickListener(this);
        iv_sub = (ImageView) findViewById(R.id.iv_sub);
        iv_sub.setOnClickListener(this);
        iv_plus = (ImageView) findViewById(R.id.iv_plus);
        iv_plus.setOnClickListener(this);

        et_assist_number = (EditText) findViewById(R.id.et_assist_number);
        et_number = (EditText) findViewById(R.id.et_number);
        tv_assist_unit = (TextView) findViewById(R.id.tv_assist_unit);
        tv_ware = (TextView) findViewById(R.id.tv_ware);


      /*  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        try {
            Class<EditText> cls = EditText.class;
            Method setSoftInputShownOnFocus;
            setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            setSoftInputShownOnFocus.setAccessible(true);
            setSoftInputShownOnFocus.invoke(et_number, false);
            setSoftInputShownOnFocus.invoke(et_assist_number, false);

        } catch (Exception e) {
            e.printStackTrace();
        }*/


    }

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;
    }

    public CartDialog(Context context, BrandInfo child) {
        super(context, R.style.dialogActivityStyle);
        brandInfo = child;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.normal_cart_two_btn_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        et_assist_number.setText(String.valueOf(brandInfo.getNum_Box()));
        et_number.setText(String.valueOf(brandInfo.getNum_Single()));
        tv_assist_unit.setText("箱(" + brandInfo.getParam() + "支)");
        int box = brandInfo.getStoreNum() / brandInfo.getParam();
        int single = brandInfo.getStoreNum() % brandInfo.getParam();
        tv_ware.setText(brandInfo.getWarehouse() + "(" + box + "箱" + single + "支)");
        //如果设置按钮的文字
        if (yesStr != null) {
            sure.setText(yesStr);
        }
        if (noStr != null) {
            cancel.setText(noStr);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_assist_sub:
                int num = Integer.valueOf(et_assist_number.getText().toString());
                if (num > 0) {
                    et_assist_number.setText(String.valueOf(--num));
                }
                break;
            case R.id.iv_assist_plus:
                int num1 = Integer.valueOf(et_assist_number.getText().toString());
                et_assist_number.setText(String.valueOf(++num1));
                break;
            case R.id.iv_sub:
                int num2 = Integer.valueOf(et_number.getText().toString());
                if (num2 > 0) {
                    et_number.setText(String.valueOf(--num2));
                }
                break;
            case R.id.iv_plus:
                int num3 = Integer.valueOf(et_number.getText().toString());
                et_number.setText(String.valueOf(++num3));
                break;

            default:
                break;
        }
    }

    public int modifyCart() {
        int box = Integer.valueOf(et_assist_number.getText().toString());
        int single = Integer.valueOf(et_number.getText().toString());
        int storeNum = new StoreNumDB().getStoreNum(brandInfo.getEncode(), brandInfo.getWarehouseId());
        /*if(box * brandInfo.getParam() + single > storeNum){
            ToastUtils.show("库存不足，请重新选择！剩余库存总量为:"+storeNum+"支");
            return 0;
        }*/
        if (box == 0 && single == 0) {
            ToastUtils.show("商品数量不能为0！");
            return 0;
        } else {
            brandInfo.setNum_Box(box);
            brandInfo.setNum_Single(single);

            int quantity;
            if (single == 0) {
                quantity = brandInfo.getParam();
            } else {
                quantity = 1;
            }

            brandInfo.setUcti(brandInfo.getUnitSalePrice() * quantity);

            String notexPrice = new PriceDB().getNotexSalePrice(brandInfo.getEncode(),brandInfo.getCustomerId());
            List<ShopTypeBean.ShopType.ProductInfo> list = new ShopsDB().findShopsById(brandInfo.getEncode());
            if (Double.valueOf(notexPrice) == 0.0 && list.size() > 0) {
                brandInfo.setUcte(Double.valueOf(list.get(0).getNonTaxSalePrice()) / ((100 + list.get(0).getTaxrate()) / 100) * quantity);
            } else {
                brandInfo.setUcte(Double.valueOf(notexPrice) * quantity);
            }

            brandInfo.setSalePriceT4(brandInfo.getUnitSalePrice());
            brandInfo.setRealUcti(brandInfo.getUcti());
            brandInfo.setRealUcte(brandInfo.getUcte());
            if (box != 0 && single != 0) {
                brandInfo.setTcti(brandInfo.getUcti() * (box * brandInfo.getParam() + single));
                brandInfo.setTcte(brandInfo.getUcte() * (box * brandInfo.getParam() + single));
            } else if (box != 0 && single == 0) {
                brandInfo.setTcti(brandInfo.getUcti() * box);
                brandInfo.setTcte(brandInfo.getUcte() * box);
            } else if (box == 0 && single != 0) {
                brandInfo.setTcti(brandInfo.getUcti() * single);
                brandInfo.setTcte(brandInfo.getUcte() * single);
            }

            brandInfo.setTaxAmount(brandInfo.getTcti() - brandInfo.getTcte());
            Log.e("modify", "brandInfo, " +brandInfo.toString());
            //BrandInfoDB user = new BrandInfoDB();
            new BrandInfoDB().modifyShop(brandInfo);
            ToastUtils.show("修改成功");
            return 1;
        }
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        void onYesClick();
    }

    public interface onNoOnclickListener {
        void onNoClick();
    }
}

